package eirkdbd.lab1.membership.functions;

import java.lang.reflect.InvocationTargetException;

import eirkdbd.lab1.data.Team;
import eirkdbd.lab1.membership.MembershipFunction;
import eirkdbd.lab1.membership.functions.helpers.LinearMembershipFunction;

/*
 * 	private String name;
 private String league;
 private int rank;
 private int games;
 private int wins;
 private int loses;
 private int scoredGoals;
 private int lostGoals;
 private int goalBalance;
 private int ties;
 private int pts;

 * 
 * 
 */

/**
 * CustomMembershipFunction <br>
 * Provides mechanism to create custom membership function <br>
 * Function is built on basis of minX and maxX (points were function value is 0
 * and 1) and name of statistic which is used to count membership degree.<br>
 * Allowed statistic names are:
 * <ul>
 * <li>Rank -> Team ranking in the league
 * <li>Games -> Number of played games
 * <li>Wins -> Number of won games
 * <li>Loses -> Number of lost games
 * <li>Ties -> Number of tied games
 * <li>ScoredGoals -> Number of scored goals
 * <li>LostGoals -> Number of lost goals
 * <li>GoalBalance -> Difference between scored and lost goals
 * <li>Pts -> Number of gained points
 * </ul>
 */
public class CustomMembershipFunction implements MembershipFunction {

	private String statisticName;
	private LinearMembershipFunction lmf;

	public CustomMembershipFunction(double maxX, double minX, String statisticName) {
		lmf = new LinearMembershipFunction(minX, maxX);
		this.statisticName = statisticName;
	}

	/**
	 * Returns degree of membership to custom group
	 */
	public double getValue(Team team) {
		int statistic = 0;
		java.lang.reflect.Method method;
		try {
			method = team.getClass().getMethod("get" + statisticName);
			statistic = (Integer) method.invoke(team);
		} catch (SecurityException e) {
			e.printStackTrace();
		} catch (NoSuchMethodException e) {
			e.printStackTrace();
		} catch (IllegalArgumentException e) {
			e.printStackTrace();
		} catch (IllegalAccessException e) {
			e.printStackTrace();
		} catch (InvocationTargetException e) {
			e.printStackTrace();
		}

		return lmf.value(statistic);
	}

}
